﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;

namespace TrackingSim.ResourceSystem
{
    public delegate void load_delegate<T>(string path, List<T> resources);
    public delegate void load_delegate_cm<T>(string path, ContentManager cm, List<T> resources);

    class ResourceManager
    {
        List<Resource> m_resources;
        load_delegate<Resource> loader;
        load_delegate_cm<Resource> loader_cm;

        public ResourceManager(load_delegate<Resource> _loader)
        {
            m_resources = new List<Resource>();
            loader = _loader;
            loader_cm = null;
        }

        public ResourceManager(load_delegate_cm<Resource> _loader)
        {
            m_resources = new List<Resource>();
            loader = null;
            loader_cm = _loader;
        }

        public void addResource(string path)
        {
            if (loader != null)
            {
                loader(path, m_resources);
            }
        }

        public void addResource(string path, ContentManager cm)
        {
            if (loader_cm != null)
            {
                loader_cm(path, cm, m_resources);
            }
        }

        public List<Resource> Resources
        {
            get { return m_resources; }
        }

        public Resource getResourceByName(string name)
        {
            foreach (Resource r in m_resources)
            {
                if (r.Name == name)
                {
                    return r;
                }
            }

            return default(Resource);
        }

        public List<Resource> getResourceGroupByName(string group_name)
        {
            List<Resource> group_list = new List<Resource>();

            foreach (Resource r in m_resources)
            {
                if (r.GroupName == group_name)
                {
                    group_list.Add(r);
                }
            }

            return group_list;
        }

        public List<int> getResourceGroupIndicesByName(string group_name)
        {
            List<int> group_list = new List<int>();

            for (int i = 0; i < m_resources.Count; ++i)
            {
                if (m_resources[i].GroupName == group_name)
                {
                        group_list.Add(i);
                }
            }

            return group_list;
        }
    }
}
